<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <h2>
        部门
      </h2> -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <deptTree :node-data="company" :is-root="true" @addDept="addDeptFn" />

        <!-- 部门展示  树形组件 -->
        <el-tree :data="depts" :props="{label:'name'}" default-expand-all :expand-on-click-node="false">
          <!-- data是 element tree组件遍历数据后台渲染时返回的数据
        就是当前节点的数据对象，这里指的是部门数据对象
         -->
          <template #default="{data}">
            <deptTree :node-data="data" :is-root="false" @delDepartment="getDepartment" @addDept="addDeptFn" @editDepartment="editDepartment" />
          </template>
        </el-tree>
        <addDept
          ref="addDept"
          :show-dialog.sync="showDialog"
          :node-data="nodeData"
          @addDepartment="getDepartment"
          @closeDialog="showDialog = false"
        />
      </el-card>

    </div>
  </div>
</template>

<script>
import deptTree from '@/views/components/dept-tree.vue'
import { getDepartment } from '@/api/department'
import { listToTreeData } from '@/utils/index'
import addDept from '@/views/components/add-dept.vue'
export default {
  components: { deptTree, addDept },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      depts: [], // 部门列表

      showDialog: false, // 添加部门弹窗的显示
      nodeData: {}
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      // 请求部门列表
      const { depts } = await getDepartment()
      this.depts = listToTreeData(depts, '')
    },

    // 添加部门
    addDeptFn(e) {
      this.showDialog = true
      this.nodeData = e
    },

    // 编辑部门
    async editDepartment(nodeData) {
      // 这里知道被点击编辑了, 就应该加载数据
      // 而且这里是父页面, 要通知子组件加载的话
      // 用 this.$refs.xxx 即可
      await this.$refs.addDept.getDepartmentDetail(nodeData.id)
      this.showDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
